#ifndef CNETWORK_H
#define CNETWORK_H

#include <QObject>
#include <QHostAddress>
#include <QUdpSocket>
#include "define_cmd.h"
#include "ttg_ips/ttgipsprotocol.h"

class CNetwork : public QObject
{
    Q_OBJECT
public:
    explicit CNetwork(QObject *parent = 0);
    ~CNetwork();

    static bool secondEncry;

    static void print_data(char *title, char *src, _int32 len);
    static bool ipAddressVaild(QString ip);

    void setDevIpAddress(QString ip, int port = NETWORK_DEVICE_PORT);
    void setDevPassword(QString password);

    void data_analyze(QByteArray bytes, QString ip, unsigned short port);
    void startBoardCast();
    void startSearchIp(QString ip);
    void devIpChange(QString oldIp, QString newIp, QString pass);
    void devBinFileUpload(QByteArray data, QString pass);
    void devPswInit(QString ip,QString pass);

    QList<QStringList *> *getNumbersSubObjfromList(QStringList *list,unsigned int number);
    void releaseQList(QList<QStringList *> *list);

    int filterUpdateB2Nphy(QString ip, QString pass, QStringList list,char canPass);
    int filterUpdateB2Ngroup(QString ip, QString pass, QStringList list, char canPass);
    int filterUpdateN2Bphy(QString ip, QString pass, QStringList list, char canPass);
    int filterUpdateN2Bgroup(QString ip,QString pass,QStringList list,char canPass);
    int filterUpdateN2Bip(QString ip, QString pass, QStringList list,char canPass);

    int devPasswordChange(QString ip, QString oldPsw, QString newPsw);

    int iPadSetUser(int cmd,QString ip,QString psw,QString user);
    int iPadSetPass(int cmd,QString ip,QString psw,QString pass);
    int iPadSetPort(int cmd,QString ip,QString psw,QString port);
    int iPadSetIp(QString ip,QString psw,QString ipadIp);
    int iPadSetNetwork(QString ip, QString psw,QByteArray mac, unsigned int mask, unsigned int gateway);
    int iPadReadNetwork(QString ip, QString psw);

signals:
    void boardcastDeviceAck(QString ip,QByteArray pass);
    void uploadFileStatus(int finish);
    void ipChangeStatus(int cmd);
    void ipInitStatus(int cmd);
    void ipadNetworkInfo(QByteArray mac,QByteArray mask,QByteArray gateway);

public slots:
    void readDataSocket();
    void udpSendToDev(char *data,quint32 len);
    void udpBordcastSendTo(char *data,quint32 len);

private:
    int checkWaitStatus(int &status);

    QUdpSocket udpSend,udpRcv;
    QHostAddress ipBoardcast,ipDev;
    int devPort;
    int _binUpdateStatus,_binUpdateHandStatus,_ipChangeStatus,
        _ipInitStatus,_filterStatus,_passChangeStatus,_ipadChangeStatus,_ipadChangeIPstatus,_ipadChangeNetworkStatus,
   _ipadReadNetworkStatus ;

    TTGIpsProtocol *_protocol;

};

#endif // CNETWORK_H
